package cn.yunlang.zhangZhongFei.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.yunlang.zhangZhongFei.entity.Award;
import cn.yunlang.zhangZhongFei.entity.WinAward;
import cn.yunlang.zhangZhongFei.service.AwardService;
import cn.yunlang.zhangZhongFei.util.JwtUtil;
import cn.yunlang.zhangZhongFei.vo.request.AwardVo;
import cn.yunlang.zhangZhongFei.vo.request.ChanceVo;
import cn.yunlang.zhangZhongFei.vo.request.TokenRequest;
import cn.yunlang.zhangZhongFei.vo.response.R;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/Award")
@Api(tags = "奖品抽奖管理", description = "奖品抽奖相关接口")
public class AwardController {

  @Resource private AwardService awardService;

  @ApiOperation(value = "分页查询奖品列表", notes = "分页查询奖品列表信息")
  @GetMapping("/page")
  public R<IPage<Award>> page(
      @ApiParam(value = "页码", required = true) @RequestParam Integer pageNo,
      @ApiParam(value = "每页大小", required = true) @RequestParam Integer pageSize,
      @ApiParam(value = "奖品名称", required = false) @RequestParam(required = false)
          String awardName) {
    return awardService.pageAwards(pageNo, pageSize, awardName);
  }

  @ApiOperation(value = "根据ID查询奖品", notes = "根据奖品ID查询单个奖品详细信息")
  @ApiImplicitParam(
      name = "awardId",
      value = "奖品ID",
      required = true,
      dataType = "Long",
      paramType = "query")
  @GetMapping("/one")
  public R<Award> one(@RequestParam Long awardId) {
    return R.success(awardService.selectId(awardId));
  }

  @ApiOperation(value = "新增奖品", notes = "新增奖品信息")
  @ApiResponses({
    @ApiResponse(code = 200, message = "新增成功"),
    @ApiResponse(code = 500, message = "新增失败")
  })
  @PostMapping("/add")
  public R<String> add(@RequestBody Award award) {
    boolean success = awardService.addAward(award);
    return success ? R.success("新增成功") : R.success("新增失败");
  }

  @ApiOperation(value = "获取抽奖机会", notes = "通过口令获取抽奖机会")
  @ApiResponses({
    @ApiResponse(code = 200, message = "口令匹配,可以参加抽奖"),
    @ApiResponse(code = 200, message = "口令不匹配")
  })
  @PostMapping("/getChance")
  public R<String> getChance(@RequestBody ChanceVo chanceVo) {
    System.out.println("m3133"+new JwtUtil());
    String uuid = new JwtUtil().getUserId();
    System.out.println("mjy-uuid:"+uuid);
    chanceVo.setUserId(Long.valueOf(new JwtUtil().getUserId()));
    String result = awardService.getChance(chanceVo);
    return R.success(result);
  }

  @ApiOperation(value = "参与抽奖", notes = "参与抽奖并记录结果")
  @ApiResponses({
    @ApiResponse(code = 200, message = "参与成功"),
    @ApiResponse(code = 500, message = "参与失败")
  })
  @PostMapping("/join")
  public R<String> join(@RequestBody AwardVo awardVo) {

    awardVo.setUserId(Long.valueOf(new JwtUtil().getUserId()));
    String result = awardService.joinAward(awardVo);
    return R.success(result);
  }

  @ApiOperation(value = "查询中奖状态", notes = "查询用户是否中奖")
  @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
  @PostMapping("/isWin")
  public R<WinAward> isWin(@RequestBody AwardVo awardVo) {

    awardVo.setUserId(Long.valueOf(new JwtUtil().getUserId()));
    WinAward winAward = awardService.checkWinStatus(awardVo);
    return R.success(winAward);
  }
}
